package com.example.text.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.text.entity.*;
import com.example.text.dao.UserDao;
import com.example.text.service.AdminRoleService;
import com.example.text.service.AdminUserRoleService;
import com.example.text.service.UserService;

import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service
public class UserServiceImpl extends ServiceImpl<UserDao, User> implements UserService  {


    //将DAO注入Service层
    @Autowired
    private UserDao userDao;

    @Autowired
    private  UserService userService;
    @Autowired
    private AdminRoleService adminRoleService;

    @Autowired
    private AdminUserRoleService adminUserRoleService;

    @Override
    public User loginIn(User requestUser) {
        String username= requestUser.getUsername();
        String password= requestUser.getPassword();
        return userDao.getInfo(username,password);
    }

    @Override
    public User getByUserName(String username) {
        return userDao.getByUserName(username);
    }

    public void editUser(User user) {
        User userInDB = userDao.getByUserName(user.getUsername());
        userInDB.setName(user.getName());
        userInDB.setMobile(user.getMobile());
        userInDB.setEmail(user.getEmail());
        userInDB.setCover((user.getCover()));
        userDao.updateById(userInDB);
        adminUserRoleService.saveRoleChanges(userInDB.getId(), user.getRoles());
    }


  public IPage<User> selectUserPage (Page<User> page){
      IPage<User> userEntityIPage= userDao.selectPageVo(page);

      List<AdminRole> roles;
      for (User user : userEntityIPage.getRecords()) {
          roles = adminRoleService.listRolesByUser(user.getUsername());
          user.setRoles(roles);
      }
      return userEntityIPage;
  }

  public User updateUserEnable(User requestUser){
      User userInDB = userDao.getByUserName(requestUser.getUsername());
      userInDB.setEnabled(requestUser.isEnabled());
      userDao.updateById(userInDB);
      return userInDB;
  }

  public  void updateUserStatus(User user,int a){

        user.setStatus(a);
        userDao.updateById(user);
  }

   public void resetPassword(User requestUser){


           User userInDB = userDao.getByUserName(requestUser.getUsername());
           String salt = new SecureRandomNumberGenerator().nextBytes().toString();
           int times = 2;
           userInDB.setSalt(salt);
           String encodedPassword = new SimpleHash("md5", requestUser.getPassword(), salt, times).toString();
           userInDB.setPassword(encodedPassword);
           userDao.updateById(userInDB);

   }

   public  void deleteUser(User requestUser){
        int id=requestUser.getId();
        userDao.deleteById(id);

        //删除对应的规则
       adminUserRoleService.remove(new QueryWrapper<AdminUserRole>().eq("uid",id));
   }

   public void deleteUsers(String[] userIds){
        userDao.deleteBatchIds(Arrays.asList(userIds));

       for(String id : userIds){
           //删除对应的规则
           adminUserRoleService.remove(new QueryWrapper<AdminUserRole>().eq("uid",id));
       }
   }



}

